// components/productList/productList.js
var {$author: $author} = require("../../components/base/authorization.js");

const app = getApp();

Component({
    /**
   * 组件的属性列表
   */
    options: {
        addGlobalClass: true
    },
    properties: {
        proData: {
            type: Object,
            observer(newVal, oldVal, changePath) {
                if (newVal && newVal.length > 0) {
                    newVal.map(x => {
                        if (x.is_mem_level == 1) {
                            if (this.data.features.memberVip == 1 && x.is_vip == 1 && this.data.vip_status == 1) {
                                x.vip_price = Number(x.vip_price) > Number(x.mem_level_price) ? x.mem_level_price : x.vip_price;
                            } else {
                                x.vip_price = x.mem_level_price;
                            }
                        }
                    });
                }
                this.setData({
                    proData: newVal
                });
            }
        },
        special_custom_name: {
            type: String
        },
        path: {
            type: String
        },
        color_main: {
            type: String
        },
        main_style: {
            type: String
        },
        quick_share: {
            type: Number
        },
        features: {
            type: Object
        },
        user_status: {
            type: Number
        },
        member_audit: {
            type: Number
        },
        vip_status: {
            type: Number
        },
        sku_display: {
            type: String
        },
        sku_num_display: {
            type: Number
        },
        sale_time_show: {
            type: Number
        }
    },
    /**
   * 组件的初始数据
   */
    data: {
        redPackageSmallFlag: false
    },
    observers() {},
    /**
   * 组件的方法列表
   */
    methods: {
        //跳转红包广场
        goToRedMarket() {
            wx.navigateTo({
                url: "/pages/discount_square/discount_square"
            });
        },
        //专场活动领取红包
        getRedPackage(e) {
            var that = this;
            var openid = wx.getStorageSync("user").openid;
            var id = e.detail.id;
            var i = e.currentTarget.dataset.index;
            $author({
                success() {
                    common.request({
                        endpoint: "Voucher/voucher/draw",
                        method: "post",
                        data: {
                            openid: openid,
                            vor_id: id
                        },
                        success: res => {
                            if (res.code == 0) {
                                if (res.message == "红包领取成功") {
                                    wx.showToast({
                                        title: res.message
                                    });
                                } else {
                                    wx.showToast({
                                        title: res.message
                                    });
                                    that.setData({
                                        redPackageSmallFlag: true
                                    });
                                }
                            }
                        },
                        fail: res => {
                            wx.showToast({
                                title: res.message
                            });
                            that.setData({
                                redPackageSmallFlag: true
                            });
                        }
                    });
                }
            });
        },
        addShoppngCar: function() {
            var myEventDetail = {
                // detail对象，提供给事件监听函数  //监听函数可以通过e.detail查看传递的数据;　　　
                hidden: false,
                text: this.data.ctext
            };
            var myEventOption = {};
            // 触发事件的选项
                        this.triggerEvent("myevent", myEventDetail, myEventOption);
        },
        share_tmp(e) {
            var myEventDetail = {
                id: e.currentTarget.dataset.id,
                item: e.currentTarget.dataset.item
            };
            this.triggerEvent("sharetmp", myEventDetail, {});
        },
        join_cart(e) {
            var myEventDetail = {
                id: e.currentTarget.dataset.id,
                single: e.currentTarget.dataset.single,
                onlyvip: e.currentTarget.dataset.onlyvip,
                minimumnum: e.currentTarget.dataset.minimumnum,
                stepnum: e.currentTarget.dataset.stepnum
            };
            this.triggerEvent("joincart", myEventDetail, {});
        },
        reduceCount(e) {
            var myEventDetail = {
                id: e.currentTarget.dataset.id,
                single: e.currentTarget.dataset.single,
                onlyvip: e.currentTarget.dataset.onlyvip,
                minimumnum: e.currentTarget.dataset.minimumnum,
                stepnum: e.currentTarget.dataset.stepnum
            };
            this.triggerEvent("reducecount", myEventDetail, {});
        },
        addCount(e) {
            var myEventDetail = {
                id: e.currentTarget.dataset.id,
                single: e.currentTarget.dataset.single,
                item: e.currentTarget.dataset.item,
                onlyvip: e.currentTarget.dataset.onlyvip,
                minimumnum: e.currentTarget.dataset.minimumnum,
                stepnum: e.currentTarget.dataset.stepnum
            };
            this.triggerEvent("addcount", myEventDetail, {});
        },
        goToDetail(e) {
            if (this.data.user_status != 2 && this.data.member_audit == 1) {
                wx.showModal({
                    title: "提示",
                    content: "你还不是会员，请先到个人中心申请吧！",
                    showCancel: false
                });
                return false;
            }
            if (e.currentTarget.dataset.url) {
                wx.navigateTo({
                    url: e.currentTarget.dataset.url + "?id=" + e.currentTarget.dataset.id
                });
            }
        },
        onPageTap: function(e) {
            app.globalData.link = e.currentTarget.dataset.url;
            wx.switchTab({
                url: e.currentTarget.dataset.url
            });
        },
        ToVideoDetail(e) {
            var id = e.currentTarget.dataset.id;
            // wx.navigateTo({
            //   url: '/pages/videoDetail/videoDetail?id=' + id,
            // })
                        wx.navigateTo({
                url: `/video/pages/goodsVideo/goodsVideo?id=${id}`
            });
        }
    },
    lifetimes: {
        ready: function() {
            let that = this;
            that.setData({
                domain: app.globalData.domain.slice(0, -10)
            });
        }
    }
});